﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;

namespace JQueryWidget.WidgetFrame
{
    public partial class LoadWidget : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(Request["typeid"]))
            {
                Response.Clear();
                Response.Write("Error");
                Response.End();
            }
            else
            {                
                string widgetUrl = (from ele in XDocument.Load(Server.MapPath("~/Data/widgets.xml")).Element("widgets").Elements("widget")
                                        where ele.Element("id").Value == Request["typeid"]
                                        select ele.Element("src").Value).FirstOrDefault();
                if (String.IsNullOrEmpty(widgetUrl))
                {
                    widgetUrl = "~/widgets/CalendarWidget.ascx";
                }

                if (Request["needWrapper"] != null && Request["needWrapper"] == "true")
                {
                    JQueryWidget.WidgetFrame.Common.IWidgetWrapper widgetWrapper = Page.LoadControl("~/Modules/WidgetWrapper.ascx") 
                        as JQueryWidget.WidgetFrame.Common.IWidgetWrapper;
                    widgetWrapper.LoadWidget(Request["typeid"]);
                    widgetContainer.Controls.Add(widgetWrapper as Control);
                }
                else
                {
                    widgetContainer.Controls.Add(Page.LoadControl(widgetUrl));
                }
            }
        }
    }
}